package com.example.demo.Service.Impl;

import com.example.demo.Entity.FSFile;
import com.example.demo.Entity.FSFolder;
import com.example.demo.Entity.RecycleFile;
import com.example.demo.Master.MasterBase;
import com.example.demo.Service.RecycleService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;

@Service
public class RecycleServiceImpl implements RecycleService {

    //显示回收站文件
    public ArrayList showRecycleFiles(String userName,MasterBase masterBase){
        ArrayList<RecycleFile> recycleFiles = new ArrayList<>();
        FSFolder rootfolder = masterBase.getRoot().getFolderByName(masterBase.getRoot().getChildFolderList(),userName);
        StringBuffer path = new StringBuffer();
        rootfolder.showRecycleFiles(recycleFiles,path);
        return recycleFiles;
    };

    //文件恢复
    public void restoreFSFile(String userName,MasterBase masterBase,String filePath,String fileName){
        FSFolder rootfolder = masterBase.getRoot().getFolderByName(masterBase.getRoot().getChildFolderList(),userName);
        String path = filePath+"/"+fileName;
        FSFile targetfile = rootfolder.getFSFileByPath(path);
        targetfile.setRecycle(false);
    };

    //彻底删除文件（filePath不含文件名，只到文件夹，例如/qqq/www/eee）
    public void destroyFile(String userName,MasterBase masterBase,String filePath,String fileName){
        FSFolder rootfolder = masterBase.getRoot().getFolderByName(masterBase.getRoot().getChildFolderList(),userName);
        FSFolder targetFolder = rootfolder.getFSFolderByPath(filePath);
        FSFile targetFile = targetFolder.getFSFileByName(fileName);
        targetFolder.getFilelist().remove(targetFile);
        String path = "/root/"+userName+filePath+"/"+fileName;
        String serverName = masterBase.getChunkInfoMap().get(path).getServerName();
        String hash = masterBase.getChunkInfoMap().get(path).getHash().toString();
        String targetPath = "D:\\root\\"+serverName+"\\"+hash;
        File file = new File(targetPath);
        file.delete();
        masterBase.getChunkInfoMap().remove(path);
    };
}
